<?php

namespace app\admin\controller;

use app\admin\service\LoginService;
use app\common\util\ErrorCode;

class PassportController extends CommonController
{
    public function initialize()
    {
        $this->noCheckTokenAction = ["login", "getConfig"];
        parent::initialize();
    }

    //登陆
    public function login()
    {
        if($this->returnData["code"] !== 0){
            return $this->return_result($this->returnData);
        }
        $service = $this->app->invokeClass(LoginService::class, [$this->returnData, $this->params]);
        $this->returnData = $service->LoginByAccount();
        return $this->return_result($this->returnData);
    }

    //登录者信息
    public function getInfo(){
        if($this->returnData["code"] !== 0){
            return $this->return_result($this->returnData);
        }
        $admin_id = $this->getSessionIdVar($this->params['token'], 'id');
        $service = $this->app->invokeClass(LoginService::class, [$this->returnData, $this->params]);
        $this->returnData = $service->getLoginInfo($admin_id);
        return $this->return_result($this->returnData);
    }

    //退出登陆
    public function logout()
    {
        if($this->returnData["code"] !== 0){
            return $this->return_result($this->returnData);
        }
        $admin_id = $this->getSessionIdVar($this->params['token'], 'id');
        if(empty($admin_id)){
            $result = ErrorCode::$errorCode['fail'];
            $result["msg"] = "已经退出登录了";
            return $this->return_result($result);
        }
        $service = $this->app->invokeClass(LoginService::class, [$this->returnData, $this->params]);
        $this->returnData = $service->logout($admin_id);
        return $this->return_result($this->returnData);
    }

    //退出登陆
    public function getConfig()
    {
        if($this->returnData["code"] !== 0){
            return $this->return_result($this->returnData);
        }
        $service = $this->app->invokeClass(LoginService::class, [$this->returnData, $this->params]);
        $this->returnData = $service->getConfig();
        return $this->return_result($this->returnData);
    }

}